/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.query;

import edu.pdx.cs.guavadata.Channel;
import edu.pdx.cs.guavadata.Transform;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author VuTran
 */
public class Insert extends Operator {

    // DML Statement for inserting rows into a table

    String table;
    List<String> columns;
    Operator child;

    public Operator getChild() {
        return child;
    }

    public void setChild(Operator child) {
        this.child = child;
    }

    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public Insert(String table, Collection<String> columns, Operator query) {
        this.table = table;
        this.columns = new ArrayList<String>(columns);
        this.child = query;
        this.parent = null;

        try {
            // Dummy check
            if (query.exposedColumns == null)
                throw new Exception("Query to produce rows does not have any exposed columns, and is of type " + query.getClass().getName() + ".");
            if (this.columns.size() != query.exposedColumns.size())
                throw new Exception("Column counts between expected columns " + this.columns.size() + " and query exposed columns " + query.exposedColumns.size() + " do not match.");
        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
    }

    @Override
    public void notifyChange(Operator old, Operator newOp) {
        if (this.child == old) {
            this.child = newOp;
            newOp.parent = this;
        }
    }

    @Override
    public boolean isQuery() {
        return false;
    }

    @Override
    public SqlText visit(Provider qv) {
        return qv.transform(this);
    }

    @Override
    public void visit(Channel c, String table) {
        c.mapAliases(this, table);
    }

    @Override
    public void visit(Transform t, Command c) {
        t.expand(this, c);
    }

    @Override
    public Operator clone() {
        return new Insert(table, columns, this.child.clone());
    }

}
